package agentes;

import modelo.Model;
import modelo.Model.Directions;
import modelo.Model.Instructions;
import modelo.info.Constants;
import modelo.simulacion.RouteGoogle;

public class DriverDecisionMaker {

	private Model modelo;
	private RouteGoogle ruta;
	private Integer currentInstruction;
	private Boolean permanentDone;
	private Integer permanentInstruction;
	
	public DriverDecisionMaker(Model model) {
		
		modelo = model;
		currentInstruction = -1;
		permanentInstruction = -1;
		permanentDone = false;
	}
	
	public RouteGoogle getRuta() {
		return ruta;
	}

	public void setRuta(RouteGoogle ruta) {
		this.ruta = ruta;
	}

	public Integer getCurrentInstruction() {
		return currentInstruction;
	}

	public void setCurrentInstruction(Integer currentInstruction) {
		this.currentInstruction = currentInstruction;
	}
	
	public Integer getPermanentInstruction() {
		return permanentInstruction;
	}

	public void setPermanentInstruction(Integer permanentInstruction) {
		this.permanentInstruction = permanentInstruction;
	}

	/*public Boolean getIsPermanentInstruction() {
		return isPermanentInstruction;
	}

	public void setIsPermanentInstruction(Boolean isPermanentInstruction) {
		this.isPermanentInstruction = isPermanentInstruction;
	}*/

	public String getNextStep() {
		
		String result = Constants.END;
		if (ruta.nextStep()) {
			result = ruta.getCurrentInstruction();
		}
		return result;
	}
	
	public Boolean isGoodRoute(int sentido,String step) {
		
		Boolean correct = false;
		if (step.contains(Constants.HEAD)) {
			if (step.contains(Constants.NORTH)) {
				if (sentido == Directions.Arriba.ordinal() || 
					sentido == Directions.ArrDer.ordinal() || 
					sentido == Directions.ArrIzq.ordinal())
					correct = true;
			}
			else if (step.contains(Constants.SOUTH)) {
				if (sentido == Directions.Abajo.ordinal() || 
					sentido == Directions.AbDer.ordinal() || 
					sentido == Directions.AbIzq.ordinal())
					correct = true;
			}
			else if (step.contains(Constants.EAST)) {
				if (sentido == Directions.Derecha.ordinal() || 
					sentido == Directions.ArrDer.ordinal() || 
					sentido == Directions.AbDer.ordinal())
					correct = true;
			}
			else if (step.contains(Constants.WEST)) {
				if (sentido == Directions.Izquierda.ordinal() || 
					sentido == Directions.ArrIzq.ordinal() || 
					sentido == Directions.AbIzq.ordinal())
					correct = true;
			}
		}
		else
			correct = true;
		
		return correct;
	}
	
	/*public Boolean isPermanentInstruction() {
		
		Boolean result = false;
		if (currentInstruction.equals(Instructions.HeadNorth)||
			currentInstruction.equals(Instructions.HeadSouth)||
			currentInstruction.equals(Instructions.HeadEast) ||
			currentInstruction.equals(Instructions.HeadWest)||
			currentInstruction.equals(Instructions.Continue)) 
				result = true;
		return result;
	}*/
	
	public Boolean isPlaceOnRoute(Integer sentido) {
		
		Boolean resultado = false;
		if (permanentInstruction >= 0)
			if (permanentInstruction.equals(Instructions.HeadNorth.ordinal())) {
				if (sentido.equals(Directions.Arriba.ordinal()) ||
					sentido.equals(Directions.ArrDer.ordinal()) ||
					sentido.equals(Directions.ArrIzq.ordinal()))
					resultado = true;
			}
			else if (permanentInstruction.equals(Instructions.HeadSouth.ordinal())) {
				if (sentido.equals(Directions.Abajo.ordinal()) ||
						sentido.equals(Directions.AbDer.ordinal()) ||
						sentido.equals(Directions.AbIzq.ordinal()))
						resultado = true;
			}
			else if (permanentInstruction.equals(Instructions.HeadEast.ordinal())) {
				if (sentido.equals(Directions.Derecha.ordinal()) ||
						sentido.equals(Directions.ArrDer.ordinal()) ||
						sentido.equals(Directions.AbDer.ordinal()))
						resultado = true;
			}
			else if (permanentInstruction.equals(Instructions.HeadWest.ordinal())) {
				if (sentido.equals(Directions.Izquierda.ordinal()) ||
						sentido.equals(Directions.ArrIzq.ordinal()) ||
						sentido.equals(Directions.AbIzq.ordinal()))
						resultado = true;
			}
			else if (permanentInstruction.equals(Instructions.Continue.ordinal())) {
				
			}
			
		return resultado;
	}

	public void testInstruction(AgentInfoOptionPlace place) {
		
		int x = place.getX();
		int y = place.getY();
		int posicion = place.getPosicion();
		String name = modelo.getDriverMap()[x][y].getDataList().get(posicion).getNombre();
		Integer sentido = 0;
		switch(currentInstruction) {
			case 0:
				sentido = modelo.getDriverMap()[x][y].getDataList().get(posicion).getSentido();
				if (sentido.equals(Directions.ArrDer.ordinal()) ||
					sentido.equals(Directions.ArrIzq.ordinal()) ||
					sentido.equals(Directions.Arriba.ordinal())) {
					String action = getNextStep();
					encodeInstruction(action);
				}
				break;
			case 1:
				sentido = modelo.getDriverMap()[x][y].getDataList().get(posicion).getSentido();
				if (sentido.equals(Directions.AbDer.ordinal()) || 
					sentido.equals(Directions.AbIzq.ordinal()) ||
					sentido.equals(Directions.Abajo.ordinal())) {
					String action = getNextStep();
					encodeInstruction(action);
				}
				break;
			case 2:
				sentido = modelo.getDriverMap()[x][y].getDataList().get(posicion).getSentido();
				if (sentido.equals(Directions.ArrDer.ordinal()) ||
					sentido.equals(Directions.AbDer.ordinal()) ||
					sentido.equals(Directions.Derecha.ordinal())) {
					String action = getNextStep();
					encodeInstruction(action);
				}
				break;
			case 3:
				 sentido = modelo.getDriverMap()[x][y].getDataList().get(posicion).getSentido();
				if (sentido.equals(Directions.ArrIzq.ordinal()) || 
					sentido.equals(Directions.AbIzq.ordinal()) ||
					sentido.equals(Directions.Izquierda.ordinal())) {
					String action = getNextStep();
					encodeInstruction(action);
				}
				break;
			case 4: //Right
				if (ruta.getCurrentInstruction().contains(name)) {
					String action = getNextStep();
					encodeInstruction(action);
				}
				break;
			case 5: // Left
				if (ruta.getCurrentInstruction().contains(name)) {
					String action = getNextStep();
					encodeInstruction(action);
				}
				break;
			case 6:
				break;
			case 7:
				break;
			case 8:
				break;
			case 9:
				break;
		}
	}
	
	public void encodeInstruction(String action) {
		
		if (action.contains(Constants.HEAD)) {
			if (action.contains(Constants.NORTH))
				currentInstruction = Instructions.HeadNorth.ordinal();
			else if (action.contains(Constants.SOUTH))
				currentInstruction = Instructions.HeadSouth.ordinal();
			else if (action.contains(Constants.EAST))
				currentInstruction = Instructions.HeadEast.ordinal();
			else if (action.contains(Constants.WEST))
				currentInstruction = Instructions.HeadWest.ordinal();
			permanentInstruction = currentInstruction;
			permanentDone = false;
		}
		else if (action.contains("Turn")) {
			if (action.contains("left"))
				currentInstruction = Instructions.TurnLeft.ordinal();
			else if (action.contains("right"))
				currentInstruction = Instructions.TurnRight.ordinal();
		}
		else if (action.contains("Slight")) {
			if (action.contains("left"))
				currentInstruction = Instructions.SlightLeft.ordinal();
			else if (action.contains("right"))
				currentInstruction = Instructions.SlightRight.ordinal();
		}
		else if (action.contains("Take")) {
			
		}
		else if (action.contains("Continue onto")) {
			currentInstruction = Instructions.Continue.ordinal();
			permanentInstruction = currentInstruction;
			permanentDone = false;
		}
		else if (action.equals(Constants.END)) {
			System.out.println("Objetivo alcanzado");
		}
		
		System.out.println("Vamos a hacer: "+action+" "+currentInstruction);
	}
	
}
